home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sysdeps / generic / strncpy.c < prev    next >
C/C++ Source or Header  |  1991-06-12  |  2KB  |  83 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <string.h>
  21. #include <memcopy.h>
  22.  
  23. char *
  24. DEFUN(strncpy, (s1, s2, n), char *s1 AND CONST char *s2 AND size_t n)
  25. {
  26.   reg_char c;
  27.   char *s = s1;
  28.  
  29.   --s1;
  30.  
  31.   if (n >= 4)
  32.     {
  33.       size_t n4 = n >> 2;
  34.  
  35.       for (;;)
  36.     {
  37.       c = *s2++;
  38.       *++s1 = c;
  39.       if (c == '\0')
  40.         break;
  41.       c = *s2++;
  42.       *++s1 = c;
  43.       if (c == '\0')
  44.         break;
  45.       c = *s2++;
  46.       *++s1 = c;
  47.       if (c == '\0')
  48.         break;
  49.       c = *s2++;
  50.       *++s1 = c;
  51.       if (c == '\0')
  52.         break;
  53.       if (--n4 == 0)
  54.         goto last_chars;
  55.     }
  56.       n = n - (s1 - s) - 1;
  57.       if (n == 0)
  58.     return s;
  59.       goto zero_fill;
  60.     }
  61.  
  62.  last_chars:
  63.   n &= 3;
  64.   if (n == 0)
  65.     return s;
  66.  
  67.   do
  68.     {
  69.       c = *s2++;
  70.       *++s1 = c;
  71.       if (--n == 0)
  72.     return s;
  73.     }
  74.   while (c != '\0');
  75.  
  76.  zero_fill:
  77.   do
  78.     *++s1 = '\0';
  79.   while (--n > 0);
  80.  
  81.   return s;
  82. }
  83.